package cn.jobstart.spider.parsing;

import cn.jobstart.common.*;
import cn.jobstart.data.DataUtil;
import cn.jobstart.enums.ResumeBasicSexFlagEnum;
import cn.jobstart.enums.TalentSubscriptionTaskChannelEnum;
import cn.jobstart.es.vo.ResumeEsMatching;
import cn.jobstart.spider.model.*;
import cn.jobstart.vo.*;
import com.alibaba.fastjson.JSONArray;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author sunke
 * @version 1.0
 * @Description 51job 人才解析
 * @date 2023-11-13 15:22
 */
public class TalentLiePinParsing implements IParsing<TalentLiePin> {


    private DataUtil dataUtil;
    private String talentSubscriptionTaskId;

    public TalentLiePinParsing(String talentSubscriptionTaskId) {
        this.dataUtil = (DataUtil) SpringUtils.getApplicationContext().getBean("dataUtil");
        this.talentSubscriptionTaskId = talentSubscriptionTaskId;
    }

    public DataUtil getDataUtil() {
        return dataUtil;
    }

    public TalentLiePinParsing setDataUtil(DataUtil dataUtil) {
        this.dataUtil = dataUtil;
        return this;
    }

    public String getTalentSubscriptionTaskId() {
        return talentSubscriptionTaskId;
    }

    public TalentLiePinParsing setTalentSubscriptionTaskId(String talentSubscriptionTaskId) {
        this.talentSubscriptionTaskId = talentSubscriptionTaskId;
        return this;
    }

    /**
     * 添加工作经历
     *
     * @param TalentLiePinItem
     * @param vo
     */
    private void addWorkExperience(TalentLiePin TalentLiePinItem, ResumeEsMatching vo) {

        if (StringUtil.isNullOrEmpty(TalentLiePinItem) || StringUtil.isNullOrEmpty(vo)) {
            return;
        }

        List<TalentLiePinWork> workList = TalentLiePinItem.getWorkExpList();
        if (StringUtil.isNullOrEmpty(workList)) {
            return;
        }
        List<ResumeWorkExperienceMiniDomainVO> workExperienceList = SkList.getInstance();

        workList.stream().forEach(item -> {
            try {
                ResumeWorkExperienceMiniDomainVO itemVo = new ResumeWorkExperienceMiniDomainVO();

                itemVo.setUnitName(StringUtil.delHTMLTag(item.getCompName()));
                itemVo.setJobName(StringUtil.delHTMLTag(item.getTitle()));

                itemVo.setStartTime(formatDate(item.getWorkStart()));
                itemVo.setEndTime(formatDate(item.getWorkEnd()));


                workExperienceList.add(itemVo);
            } catch (Exception e) {

            }


        });


        if (StringUtil.isNotObjEmpty(workExperienceList)) {

            vo.setWorkExperience(JSONArray.toJSONString(workExperienceList));
            vo.setWorkExperienceList(workExperienceList);


        }


    }

    private Date formatDate(String time) {

        return formatDate(time, "yyyy.MM");

    }

    private Date formatDate(String time, String format) {

        if (StringUtil.isNullOrEmpty(time)) {
            return null;
        }
        if (StringUtil.isNullOrEmpty(format)) {
            format = "yyyy.MM";
        }
        if ("至今".equalsIgnoreCase(time)) {
            return new Date();
        }
        try {
            return YXDate.getString2Date(time, format);
        } catch (Exception e) {

        }
        return null;

    }





    /**
     * 添加教育经历
     *
     * @param TalentLiePinItem
     * @param vo
     */
    private void addEducationalExperience(TalentLiePin TalentLiePinItem, ResumeEsMatching vo) {

        if (StringUtil.isNullOrEmpty(TalentLiePinItem) || StringUtil.isNullOrEmpty(vo)) {
            return;
        }

        List<TalentLiePinEdu> educationList = TalentLiePinItem.getEduExpList();
        if (StringUtil.isNullOrEmpty(educationList)) {
            return;
        }
        List<ResumeEducationalExperienceMiniDomainVO> educationalExperienceList = SkList.getInstance();

        educationList.stream().forEach(item -> {

            ResumeEducationalExperienceMiniDomainVO itemVo = new ResumeEducationalExperienceMiniDomainVO();

            itemVo.setTitle(StringUtil.delHTMLTag(item.getSchoolName()));
            itemVo.setUnitName(StringUtil.delHTMLTag(item.getMajor()));
            itemVo.setUnitType(item.getEduDegreeName());
            itemVo.setSchoolLabelFlag(dataUtil.getSchoolLabelBySchoolName(item.getSchoolName()));

            itemVo.setStartTime(formatDate(item.getEduStart()));
            itemVo.setEndTime(formatDate(item.getEduEnd()));


            educationalExperienceList.add(itemVo);



        });


        if (StringUtil.isNotObjEmpty(educationalExperienceList)) {

            vo.setEducationalExperience(JSONArray.toJSONString(educationalExperienceList));
            vo.setSchool(educationalExperienceList.get(0).getTitle());
            vo.setSpeciality(educationalExperienceList.get(0).getUnitName());
            vo.setEducationalExperienceList(educationalExperienceList);

        }


    }



    /**
     * 添加能力
     *
     * @param talentLiePinItem
     * @param vo
     */
    private void addCapabilityLabel(TalentLiePin talentLiePinItem, ResumeEsMatching vo) {

        if (StringUtil.isNullOrEmpty(talentLiePinItem) || StringUtil.isNullOrEmpty(vo)) {
            return;
        }

        String[] itskillList = talentLiePinItem.getSkillTags();
        if (StringUtil.isNullOrEmpty(itskillList)) {
            return;
        }

        List<ResumeCapabilityLabelMiniDomainVO> capabilityLabelList = SkList.getInstance();

        Arrays.stream(itskillList).forEach(item -> {

            try {
                ResumeCapabilityLabelMiniDomainVO itemVo = new ResumeCapabilityLabelMiniDomainVO();
                itemVo.setText(StringUtil.delHTMLTag(item));

                capabilityLabelList.add(itemVo);

            } catch (Exception e) {


            }


        });
        if (StringUtil.isNotObjEmpty(capabilityLabelList)) {
            vo.setCapabilityLabel(JSONArray.toJSONString(capabilityLabelList));
            vo.setCapabilityLabelList(capabilityLabelList);
        }


    }

    /**
     * 添加基础信息
     *
     * @param liePinItem
     * @param vo
     */
    private void addBasic(TalentLiePin liePinItem, ResumeEsMatching vo) {

        if (StringUtil.isNullOrEmpty(liePinItem) || StringUtil.isNullOrEmpty(vo)) {
            return;
        }



        //将年龄传过去 ，
        vo.setThirdAge(NumberUtil.parseInt(liePinItem.getAge(),-1));

        vo.setThirdAccountId(liePinItem.getUsercId());

        vo.setExceptCity(liePinItem.getWantDq());
        vo.setCategory(liePinItem.getWantJobTitle());

        vo.setSource(TalentSubscriptionTaskChannelEnum.LIE_PIN.getType());
        vo.setTitle(liePinItem.getResName() + "的简历");
        vo.setMarriage("保密");
        vo.setMarriageFlag(0);

        vo.setBirthPlace(liePinItem.getResDqName());
        vo.setTruename(liePinItem.getResName() );
        vo.setSex(liePinItem.getResSex());
        int sexFlag = 0;
        if ("男".equalsIgnoreCase(liePinItem.getResSex())) {
            sexFlag = ResumeBasicSexFlagEnum.MALE.getType().intValue();
        } else if ("女".equalsIgnoreCase(liePinItem.getResSex())) {
            sexFlag = ResumeBasicSexFlagEnum.FEMALE.getType().intValue();
        }
        vo.setSexFlag(sexFlag);
        int age = NumberUtil.parseInt(liePinItem.getAge());
        if (age > 0) {
            vo.setBirthday(YXDate.addYear(new Date(), -age));
        }
        vo.setLivePlace(liePinItem.getResDqName());
        vo.setAge(age);

        String resModifytime=liePinItem.getResModifytime();
        if(StringUtil.isNotObjEmpty(resModifytime)){
            vo.setResumeCreateTime(YXDate.getString2Date(resModifytime,"yyyy-MM-dd"));
            vo.setResumeModifyTime(vo.getResumeCreateTime());
        }else{
            vo.setResumeCreateTime(new Date());
            vo.setResumeModifyTime(new Date());
        }

        vo.setJobYear(liePinItem.getWorkYearsShow());
        vo.setEducation(liePinItem.getResEdulevelName());



    }



    /**
     * 解析爬虫过来的数据
     *
     * @param liePinItem
     * @return
     */
    @Override
    public ResumeEsMatching parsing(TalentLiePin liePinItem) {
        if (StringUtil.isNullOrEmpty(liePinItem)) {
            return null;
        }

        ResumeEsMatching vo = new ResumeEsMatching();
        vo.setTalentSubscriptionTaskId(talentSubscriptionTaskId);
        String resumeId = IdGen.uuid();
        vo.setId(resumeId);

        //基本信息
        addBasic(liePinItem, vo);
        
        

        //添加教育
        addEducationalExperience(liePinItem, vo);



        //添加工作经历
        addWorkExperience(liePinItem, vo);

        //技能
        addCapabilityLabel(liePinItem,vo);

        return vo;
    }
}
